package org.dromara.wms.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.web.core.BaseService;
import org.dromara.wms.domain.WmsGroup;
import org.dromara.wms.domain.WmsGroupProduct;
import org.dromara.wms.domain.WmsProduct;
import org.dromara.wms.mapper.WmsGroupProductMapper;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * 产品分组Service业务层处理
 *
 */
@RequiredArgsConstructor
@Service
public class WmsGroupProductServiceImpl extends BaseService {

  private final WmsGroupProductMapper baseMapper;
  private final WmsGroupServiceImpl wmsGroupService;
  private final WmsProductServiceImpl wmsProductService;

  /**
   * 查询产品从列表
   */
  public TableDataInfo<WmsGroupProduct> queryPageList(WmsGroupProduct bo, PageQuery pageQuery) {
    Page<WmsGroupProduct> result = baseMapper.queryPageList(pageQuery.build(), bo);
    return TableDataInfo.build(result);
  }

  /**
   * 查询产品分组列表
   */
  public List<WmsGroupProduct> queryList(WmsGroupProduct bo) {
      LambdaQueryWrapper<WmsGroupProduct> lqw = buildQueryWrapper(bo);
      return baseMapper.selectList(lqw);
  }

  private LambdaQueryWrapper<WmsGroupProduct> buildQueryWrapper(WmsGroupProduct bo) {
      LambdaQueryWrapper<WmsGroupProduct> lqw = Wrappers.lambdaQuery();
      lqw.like(bo.getGroupId() != null, WmsGroupProduct::getGroupId, bo.getGroupId());
      lqw.eq(bo.getProductId() != null, WmsGroupProduct::getProductId, bo.getProductId());
      return lqw;
  }

  /**
   * 新增产品分组
   */
  public Boolean insertByBo(WmsGroupProduct[] bo) {
    Collection<WmsGroupProduct> collection = new ArrayList<>(Arrays.asList(bo));
    WmsGroupProduct groupProduct = new WmsGroupProduct();
    groupProduct.setGroupId(bo[0].getGroupId());

    WmsGroup wmsGroup = wmsGroupService.queryById(bo[0].getGroupId());
    long groupGount = countStr(wmsGroup.getAttrValueName());

    LambdaQueryWrapper<WmsGroupProduct> lqw = buildQueryWrapper(groupProduct);
    List<WmsGroupProduct> oldList = baseMapper.selectList(lqw);
    for (WmsGroupProduct wmsGroupProduct : collection) {
      boolean inGroup = true;
      for (WmsGroupProduct product : oldList) {
        if (Objects.equals(product.getGroupId(), wmsGroupProduct.getGroupId()) && Objects.equals(product.getProductId(), wmsGroupProduct.getProductId())){
          inGroup = false;
        }
      }
      if (inGroup){
        WmsProduct wmsProduct = wmsProductService.queryById(wmsGroupProduct.getProductId());
        if (wmsProduct.getAttrValue() == null && groupGount > 0 || wmsProduct.getAttrValue() != null && groupGount == 0){
          throw new WmsStockService.emptyException(
            wmsGroupProduct.getProductName()+wmsGroupProduct.getProductId()+" 该产品规格与当前产品组不匹配！");
        }
        long count = countStr( wmsProduct.getAttrValue());
        if (groupGount == count)
          baseMapper.insert(wmsGroupProduct);
        else
          throw new WmsStockService.emptyException(
            wmsGroupProduct.getProductName()+wmsGroupProduct.getProductId()+" 该产品规格与当前产品组不匹配！");
      }
    }
    return true;
  }

  /**
   * 修改产品分组
   */
  public Boolean updateByBo(WmsGroupProduct bo) {
      return baseMapper.updateById(bo) > 0;
  }

  /**
   * 删除产品分组
   */
  public Boolean delete(WmsGroupProduct bo) {
    LambdaQueryWrapper<WmsGroupProduct> lqw = buildQueryWrapper(bo);
    return baseMapper.delete(lqw) > 0;
  }

  public WmsGroupProduct getOneByProductId(Long id){
    WmsGroupProduct bo = new WmsGroupProduct();
    bo.setProductId(id);
    LambdaQueryWrapper<WmsGroupProduct> lqw = buildQueryWrapper(bo);
    return baseMapper.selectOne(lqw);
  }

  public WmsGroupProduct getById(Long id){
    WmsGroupProduct bo = new WmsGroupProduct();
    bo.setGroupId(id);
    LambdaQueryWrapper<WmsGroupProduct> lqw = buildQueryWrapper(bo);
    if (baseMapper.selectList(lqw).size() > 0){
      return baseMapper.selectList(lqw).get(0);
    }else
      return null;
  }

  public Long getAttrCount(Long id){
    WmsGroupProduct res = getOneByProductId(id);
    WmsGroup wmsGroup = wmsGroupService.queryById(res.getGroupId());
    Long count = countStr(wmsGroup.getAttrValueName());
    return count;
  }

  public Long countStr(String str){
    long count = str.chars()
      .filter(c -> c == '|')
      .count();
    return count;
  }
}
